package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.PrePayListAdapter;
import com.jiayang.hhotel.bean.CheckOutMethodModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NowLiveDetailsModel;
import com.jiayang.hhotel.bean.PrePayModel;
import com.jiayang.hhotel.presenter.PrePayPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.view.CanNestingListView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 预付页面(现住查询详情)
 */

public class PrePayActivity extends XActivity<PrePayPresenter> implements View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private CanNestingListView listView;
    private TextView chinese,english;//中文/英文算法
    private TextView shouldPay;//应付
    private Button pay;//确认支付

    private ScrollView sv;

    private PrePayListAdapter adapter;

    private NowLiveDetailsModel model;
    private int index;//当前显示的是哪位客人下标

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.pre_pay),R.color.orange_color,null,null);
        model = (NowLiveDetailsModel) getIntent().getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);
        index = getIntent().getExtras().getInt(Constant.INTENT_INT_KEY);
        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        listView = (CanNestingListView) findViewById(R.id.lv);
        chinese = (TextView) findViewById(R.id.tv_chinese);
        english = (TextView) findViewById(R.id.tv_english);
        shouldPay = (TextView) findViewById(R.id.tv_should_pay);
        pay = (Button) findViewById(R.id.bt_enterMine);

        sv = (ScrollView) findViewById(R.id.sc);

        pay.setOnClickListener(this);

//        CommonUtil.showProgressDialog(this,R.string.loading);此处添加对话框后会在前一页面显示。原因未知
        sv.setVisibility(View.GONE);
        getP().getPrePayList(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getPreception2ListDto().get(index).getGstacct(),model.getPreception1ListDto().get(0).getCroNum(),model.getPreception1ListDto().get(0).getRoom(),1);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_pre_pay;
    }

    @Override
    public PrePayPresenter newP() {
        return new PrePayPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_enterMine://确认支付
                if(adapter != null){
                    CommonUtil.showProgressDialog(this,R.string.loading);
                    getP().asyncCheckOutMethodListLoad(Constant.LAUGUAGE_FLAG_CHINESE,3,-1,-1,-1,adapter.getAmount());
                }
                break;
        }
    }

    public void initPayMethodCreate(List<CheckOutMethodModel.PCashierPayListDtosBean> list){
        getP().checkOutMethodDialog(this,list);
    }

    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        LogUtil.showLog("error="+ StringUtil.errMsg(state));
        CommonUtil.dismissProgressDialog(this);
        sv.setVisibility(View.GONE);
        layout.setVisibility(View.VISIBLE);
        switch (state){
            case 1002://没有数据
                icon.setImageResource(Constant.NO_DATA_PICTURE);
                tv.setText(getString(R.string.no_data));
                icon.setOnClickListener(null);
                break;
            case -1://服务器错误
                icon.setImageResource(Constant.NET_ERROR_PICTURE);
                tv.setText(getString(R.string.service_error));
                icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sv.setVisibility(View.VISIBLE);
                        layout.setVisibility(View.GONE);
                        CommonUtil.showProgressDialog(PrePayActivity.this,R.string.loading);
                        getP().getPrePayList(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getPreception2ListDto().get(0).getGstacct(),model.getPreception1ListDto().get(0).getCroNum(),model.getPreception1ListDto().get(0).getRoom(),1);
                    }
                });
                break;
            default://其他错误(可以刷新)
                icon.setImageResource(Constant.TIME_OUT_PICTURE);
                tv.setText(getString(R.string.net_error));
                icon.setOnClickListener(null);
                break;
        }
    }

    public void setAdapter(List<PrePayModel.PDepositListDtoBean> list){
        CommonUtil.dismissProgressDialog(this);
        if(adapter==null){
            adapter = new PrePayListAdapter(this,list);
            listView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        sv.setVisibility(View.VISIBLE);
        shouldPay.setText(getString(R.string.should_pay)+getString(R.string.space)+adapter.getAmount()+getString(R.string.yuan));
    }
}
